<?php

namespace app\home\bussiness;


use app\home\model\User;
use think\facade\Db;
class Login
{

    public static function code($mobile){
        //生成验证码，并存入缓存
        $code=random_int(1000,9999);
        $key=substr($mobile,-4,4);
        cache($key,$code,60);
        //返回验证码到前端
        return successes('发送成功',['code'=>$code]);
    }

    /**
     * 注册中的用户添加
     * @param $data
     * @return \think\response\Redirect
     */
    public static function userAdd($data){
        try {
            //注册
            $data['password']=md5($data['password']);
            // dd($data);
            unset($data['code']);
            $res=User::create($data);
            //密码加密
            if($res){
                //加密
                $phone=substr_replace($data['phone'],'****','3',4);
                //设置session
                session('account',$phone);
                return redirect('list');
            }
        }catch (\ErrorException $e){
            return "<script>alert($e->getMessage());window.location.href='register'</script>";
        }

    }

    public static function pic_font($file,$extension){

        $image = \think\Image::open($file);
        // 给原图左上角添加水印并保存water_image.png
        $image->text('1910A',getcwd().'/font/1.ttf',20,'#ffffff')->save('storage/front/user/img/pic_font/'.time().'.'.$extension);
        $path='storage/front/user/img/pic_font/'.time().'.'.$extension;
        return $path;

    }
    //添加图片水印
    public static function pic_pic($file,$extension){

        //加水印的图保存本地
        $savename = \think\facade\Filesystem::disk('public')->putFile( '/front/user/img/pic-pic',$file);

        $image = \think\Image::open($file)
            //水印图
            ->water(root_path().'/public/music.png',1,80)//一定要加root_path（），这个路径一定要写对
            ->save('storage/'.$savename);  //别忘了加storage
        $path='storage/'.$savename;
       return $path;
    }

    /**
     * 第三方登录后
     * 用户添加
     *
     */
    public static function add($data){
        //判断是否有数据
        $result=User::where('openid',$data['openid'])->find();
            if($result){
                session('account',$data['nickname']);
                return true;
            }
        //执行添加
        $res=User::create($data);
        //进行登录
        if($res){
            session('account',$data['nickname']);
            return true;
        }else{
            return false;
        }
    }



}